﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(PackageLabelData))]
public class PackageLabelDataInspector : Editor
{
    Vector2 mPos = Vector2.zero;
    int index = 0;

    int lookStat = 0;
    int lookStat_All = 0;
    int lookStat_Lock = 1;
    int lookStat_UnLock = 2;

    static GUIStyle _selStyle = null;
    static GUIStyle selStyle
    {
        get
        {
            if (_selStyle == null)
            {
                _selStyle = new GUIStyle("ButtonMid");
                _selStyle.normal.textColor = new Color(46f / 256f, 163f / 256f, 256f / 256f, 256f / 256f);
            }

            return _selStyle;
        }
    }

    static GUIStyle _defaultStyle = null;
    static GUIStyle defaultStyle
    {
        get
        {
            if (_defaultStyle == null)
            {
                _defaultStyle = new GUIStyle("ButtonMid");
            }

            return _defaultStyle;
        }
    }

    override public void OnInspectorGUI()
    {
        //base.OnInspectorGUI();

        PackageLabelData tar = target as PackageLabelData;

        Undo.RecordObject(tar, "PackageLabelData");

        GUILayout.BeginHorizontal();

        if (GUILayout.Button("清理", GUILayout.Width(46)))
            Clear();

        GUIStyle showStyle = lookStat == 0 ? selStyle : defaultStyle;
        if (GUILayout.Button("全部列表", showStyle, GUILayout.Width(82)))
            lookStat = 0;

        showStyle = lookStat == 1 ? selStyle : defaultStyle;
        if (GUILayout.Button("锁定列表", showStyle, GUILayout.Width(82)))
            lookStat = lookStat_Lock;

        showStyle = lookStat == 2 ? selStyle : defaultStyle;
        if (GUILayout.Button("非锁定列表", showStyle, GUILayout.Width(82)))
            lookStat = lookStat_UnLock;

        if (GUILayout.Button("保存", GUILayout.Width(46)))
        {
            tar.Save();
            EditorUtility.DisplayDialog("提示", "已保存", "确定");
        }
           
        GUILayout.EndHorizontal();

        mPos = GUILayout.BeginScrollView(mPos);

        index = 0;
        foreach (var itm in tar.Target)
        {
            bool res = DrawItem(itm.Key,itm.Value);
            index++;
            if (!res) break;
        }

        GUILayout.EndScrollView();
    }

    public void Clear()
    {
        PackageLabelData tar = target as PackageLabelData;

        List<string> keys = tar.GetKeys();
        int cnt = keys.Count;
        for (int i = 0; i < cnt; i++)
        {
            if (!tar.HaveLock(keys[i]))
            {
                tar.Del(keys[i]);
                i--;
                cnt--;
            }

        }

        tar.Save();
    }

    public bool DrawItem(string key,string value)
    {
        PackageLabelData tar = target as PackageLabelData;

        bool haveLock = tar.HaveLock(key);

        if (lookStat == lookStat_Lock)
            if (!haveLock)
                return true;

        if (lookStat == lookStat_UnLock)
            if (haveLock)
                return true;

        using (var scope = new EditorGUILayout.HorizontalScope())
        {
            GUI.Box(scope.rect, new GUIContent());
            //if(GUI.Button(scope.rect,new GUIContent()))
            //{
            //    Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object>(key);
            //}

            GUILayout.Box(index.ToString());

            string lockLab = !haveLock ? "锁定" : "解锁";
                
            if (GUILayout.Button(lockLab, GUILayout.Width(36)))
            {
                if (!haveLock)
                    tar.AddLock(key);
                else
                    tar.RemoveLock(key);

                tar.Save();
            }

            GUILayout.Label(key);
            GUILayout.Label(value,GUILayout.Width(75));

            if(GUILayout.Button("x", GUILayout.Width(24)))
            {
                if (tar.HaveLock(key))
                {
                    EditorUtility.DisplayDialog("提示", "已锁定 请先解锁", "确定");
                    return true;
                }
                tar.Del(key);
                tar.Save();
                return false;
            }

            if (GUILayout.Button("Go", GUILayout.Width(46)))
            {
                Selection.activeObject = AssetDatabase.LoadAssetAtPath<Object>(key);
            } 
        }

        return true;
    }
}
